package org.arch_learn.io.netty_demo.unpacking_demo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * 自定义处理handler
 * ChannelInboundHandler 主要处理的是消息进站
 */
public class NettyServerHandler implements ChannelInboundHandler {
    public int count;

    /**
     * 处理通道读取事件
     *
     * @param channelHandlerContext
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object msg) throws Exception {
        //使用解码器前
        //ByteBuf byteBuf = (ByteBuf) msg;
        //String messageText = new String(byteBuf.array());
        //System.out.println("客户端发送过来的消息：" + byteBuf.toString(CharsetUtil.UTF_8));
        //使用解码器之后,msg参数是已经结果解码器解码完成的字符串
//        System.out.println("客户端发过来的消息：" + msg);
//        String msgText = (String) msg;
//        System.out.println("客户端发过来的消息长度为：" + msgText.length());
//        System.out.println("读取次数：" + (++count));

        ByteBuf byteBuf = (ByteBuf) msg;
        System.out.println("客户端发过来的消息长度为：" + byteBuf.readableBytes());
        System.out.println("读取次数：" + (++count));
    }

    /**
     * 处理通道读取完成事件
     *
     * @param channelHandlerContext
     * @throws Exception
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext channelHandlerContext) throws Exception {
        //使用编码器前
        //channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("你好客户端!",
        //CharsetUtil.UTF_8));//消息出站
        //使用编码器后
        ByteBuf byteBuf = Unpooled.copiedBuffer("你好Netty客户端！",
                CharsetUtil.UTF_8);
        ChannelFuture channelFuture = channelHandlerContext.writeAndFlush(byteBuf);
        if (channelFuture.isSuccess()) {
            System.out.println("消息发送成功！");
        } else {
            System.out.println("消息发送失败！");
        }
    }

    /**
     * 处理通道异常事件
     *
     * @param channelHandlerContext
     * @param throwable
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable) throws Exception {
        throwable.printStackTrace();
        channelHandlerContext.close();
    }

    @Override
    public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void channelUnregistered(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {

    }


    @Override
    public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {

    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {

    }


}
